'''
Author: your name
Date: 2021-06-25 11:53:51
LastEditTime: 2021-11-04 18:34:13
LastEditors: your name
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/user/service.py
'''
import json
from app import db
from .model import User


class UserService():
    #保存用户数据
    def save(user):
        db.session.add(user)
        db.session.commit()

    '''
    更新用户信息
    '''
    def update(user):
        tmp_user = User.query.get(user.user_pk)
        # tmp_user.user_pk=user_pk
        tmp_user.user_loginname = user.user_loginname
        tmp_user.user_password =  user.user_password
        tmp_user.user_name = user.user_name
        tmp_user.user_sex = user.user_sex
        tmp_user.user_idcard = user.user_idcard
        tmp_user.user_phone = user.user_phone
        tmp_user.user_birthday = user.user_birthday
        tmp_user.user_email = user.user_email
        tmp_user.user_intime = user.user_intime
        tmp_user.fk_role_pk = user.fk_role_pk
        tmp_user.fk_organ_pk = user.fk_organ_pk
        tmp_user.user_status = user.user_status
        db.session.commit()

    #获取用户数据列表
    def list():
        users = User.query.all()
        return users
    #分页获取用户数据列表
    def pages(page):
        pages = User.query.paginate(int(page),30,False) #flask_sqlalchemy提供分页方法.paginate
        return pages
    #更具用户主键删除用户信息
    def delete(user_pk):
        user = User.query.get(user_pk)
        db.session.delete(user)
        db.session.commit()#必须要有db.session.commit,否则数据无法删除
    
    #修改用户状态
    def upQTS(user_pk,user_status):
        role = User.query.get(user_pk)
        role.user_status = user_status
        db.session.commit()